UILoading = UIBase.New(PanelNames.UILoading)

local updateTime = 0.1
local rate = 5

function UILoading:Init(callback, isReal)
    self.callback = callback
    self.isReal = isReal
    self:RegisterEvent(UIEvent.FreshLoadingProgress, self.Update)
end

function UILoading:OnCreate()
    self.sliderProgress = self:GetChild("Bar_Lod")
    self.progress = 0
    self:Refresh()
end

function UILoading:InitView()
    self:Refresh()
end

function UILoading:Refresh()
    if not self.isReal then
        self.simulateTimer = UTimer.TimeCall(function()
            self.progress = self.progress + rate
            if self.progress >= 100 then
                self.progress = 100
                UTimer.Remove(self.simulateTimer)
                self.simulateTimer = nil
                if self.callback then
                    self.callback()
                end
                closeUI(PanelNames.UILoading)
            else
                self:UpdateProgress()
            end
        end, nil, updateTime, 100)
    end
end

function UILoading:Update(value)
    self.sliderProgress.value = value
end

function UILoading:UpdateProgress()
    self.sliderProgress.value = self.progress
end

function UILoading:OnDestroy()
    UTimer.Remove(self.simulateTimer)
    self.simulateTimer = nil
end

return UILoading